public class StudentText {
    public static void main(String[] args) {
        Student[] arr = new Student[3];
        Student stu1 = new Student(1, "张三", 20);
        Student stu2 = new Student(2, "李四", 21);
        Student stu3 = new Student(3, "王五", 22);
        arr[0] = stu1;
        arr[2] = stu2;
        arr[2] = stu3;
        Student stu4 = new Student(4, "赵六", 23);
        boolean flag = false;
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == null) {
                break;
            }
            if (stu4.getId() == arr[i].getId()) {
                flag = true;
                break;
            }
        }
        if (flag) {
            System.out.println("该id已存在,请重新输入");
        } else {
            for (int i = 0; i < arr.length; i++) {
                if (arr[i] != null) {
                    count++;
                }
            }
            if (count < 3) {
                arr[count] = stu4;
                for (int i = 0; i < arr.length; i++) {
                    if (arr[i] == null) {
                        break;
                    }
                    System.out.println(arr[i].getId() + "," + arr[i].getName() + "," + arr[i].getAge());
                }
                System.out.println("删除后");
                arr = DeleteArr(arr, 1);
                for (int i = 0; i < arr.length; i++) {
                    if(arr[i] != null)
                    System.out.println(arr[i].getId() + "," + arr[i].getName() + "," + arr[i].getAge());
                }
                System.out.println("查询");
                InquireArr(arr,1);
            } else {
                Student[] newArr = SetNewArr(arr, stu4);
                for (int i = 0; i < newArr.length; i++) {
                    System.out.println(newArr[i].getId() + "," + newArr[i].getName() + "," + newArr[i].getAge());
                }
                System.out.println("删除后");
                newArr = DeleteArr(newArr, 1);
                for (int i = 0; i < newArr.length; i++) {
                    if(newArr[i] != null)
                    System.out.println(newArr[i].getId() + "," + newArr[i].getName() + "," + newArr[i].getAge());
                }
                System.out.println("查询");
                InquireArr(newArr,1);
            }
        }
    }
public static Student[] SetNewArr(Student[] arr, Student stu4) {
    Student[] NewArr = new Student[4];
    for (int i = 0; i < arr.length; i++) {
        if(arr[i] != null)
        NewArr[i] = arr[i];
    }
    NewArr[3] = stu4;
    return NewArr;
}

public static Student[] DeleteArr(Student[] arr, int id) {
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] != null && arr[i].getId() == id) {
                arr[i] = null;
                return arr;
        }
    }
    System.out.println("不存在该id,删除失败");
    return arr;
}
public static void InquireArr(Student[] arr,int id) {
        int i =0;
    boolean flage = false;
    for (i = 0; i < arr.length; i++) {
        if(arr[i] != null && arr[i].getId() == id)
        {
            flage = true;
            arr[i].setAge(arr[i].getAge() + 1);
            break;
        }
    }
    if(flage)
    {
        System.out.println(arr[i].getId() + "," + arr[i].getName() + "," + arr[i].getAge());
    }else {
        System.out.println("没有查到该id");
    }
}
}
